<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ expectedData, keysdData } = {}) {
      this.chart.setOption({
        backgroundColor: 'rgba(0,0,0,0)',
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          },
          formatter: "{b} <br/> {a1}: {c} "
        },
        legend: {
          data: ['line', '日活跃数'],
          textStyle: {
            color: '#ccc'
          }
        },
        grid: {
          left: 10,
          right: 20,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        xAxis: {
          data: keysdData,
          axisLine: {
            lineStyle: {
              color: '#ccc'
            }
          },
          splitArea: {
            show: false
          }
        },
        yAxis: {
          minInterval: 1,
          splitLine: { show: false },
          axisLine: {
            lineStyle: {
              color: '#ccc'
            }
          },
          splitArea: {
            show: false
          }
        },
        series: [{
          name: 'line',
          type: 'line',
          data: expectedData,
          itemStyle: {
            color: '#7F292D',
            
          },
          
        }, {
          name: '日活跃数',
          type: 'bar',
          barWidth: 10,
          itemStyle: {
            normal: {
              label: {
                show: true,		//开启显示
                position: 'top',	//在上方显示
                textStyle: {	    //数值样式
                  color: '#ffc72b',
                  fontSize: 10
                }
              },
              color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [{
                  offset: 0,
                  color: '#00d386' // 0% 处的颜色
                }, {
                  offset: 1,
                  color: '#0076fc' // 100% 处的颜色
                }],
                globalCoord: false // 缺省为 false
              },
              barBorderRadius: 15,
            }
          },
          data: expectedData,
          animationDuration: 3800,
          animationEasing: 'cubicInOut'
        }]

      })
    }
  }
}
</script>
